﻿//using KingerRobot.Logging;
using KingerRobot.MexicoSailunWMS.Spi;
using System.Linq;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace KingerRobot.MexicoSailunWMS.SpiService
{
    public class BHistoryQuery : IHistoryQuery
    {


        public List<PORcsLocation> GetAllInNoTaskLocationsV2()
        {

            var db = Db.Default;
            var result = db.RcsLocations.FromSqlRaw(@"SELECT * 
                    FROM [rcs_location] [r] with(nolock)
                    WHERE [r].[loc_type] = 33 AND [r].[use_flag] = 1 
                    AND [r].[loc_enable] = 1 AND [r].[err_in_has] = 0 AND [r].[err_out_empty] = 0 AND [r].[loc_has_sku] = 0
                    AND [r].[task_occupy]=0").ToList();

            //var result = GetAllNoHasLocationsISLoc()
            //    .Where(t => t.InEnable == 1 && t.TaskOccupy == 0)
            //    .ToList();
            //return result;



            //List<PORcsLocation> result = new List<PORcsLocation>();
            //var dbHelper = new DbHelper();
            //var sql = new StringBuilder();
            //dbHelper.CommandText = sql.ToString();
            //dbHelper.CommandType = System.Data.CommandType.Text;
            //var data = dbHelper.GetXDataTable();
            //if (data is not null && data.Rows.Length > 0)
            //{
            //    for (int i = 0; i < data.Rows.Length; i++)
            //    {
            //        PORcsLocation location = new PORcsLocation();
            //        var row = data.DataTable.Rows[i];
            //        location.Id = !string.IsNullOrWhiteSpace(row.Value(nameof(location.Id))) ? Convert.ToInt64(row.Value(nameof(location.Id))) : 0;
            //        location.ErrInHas = !string.IsNullOrWhiteSpace(row.Value(nameof(location.ErrInHas))) ? Convert.ToInt32(row.Value(nameof(location.ErrInHas))) : 0;
            //        location.ErrOutEmpty = !string.IsNullOrWhiteSpace(row.Value(nameof(location.ErrOutEmpty))) ? Convert.ToInt32(row.Value(nameof(location.ErrOutEmpty))) : 0;
            //        location.GroupId = !string.IsNullOrWhiteSpace(row.Value(nameof(location.GroupId))) ? Convert.ToInt64(row.Value(nameof(location.GroupId))) : 0;
            //        location.GroupIndex = !string.IsNullOrWhiteSpace(row.Value(nameof(location.GroupIndex))) ? Convert.ToInt32(row.Value(nameof(location.GroupIndex))) : 0;
            //        location.InEnable = !string.IsNullOrWhiteSpace(row.Value(nameof(location.InEnable))) ? Convert.ToInt32(row.Value(nameof(location.InEnable))) : 0;
            //        location.LocCode = row.Value(nameof(location.LocCode));
            //        location.LocEnable = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocEnable))) ? Convert.ToInt32(row.Value(nameof(location.LocEnable))) : 0;
            //        location.LocHasSku = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocHasSku))) ? Convert.ToInt32(row.Value(nameof(location.LocHasSku))) : 0;
            //        location.LocHasSkuTime = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocHasSkuTime))) ? Convert.ToDateTime(row.Value(nameof(location.LocHasSkuTime))) : null;
            //        location.LocHeight = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocHeight))) ? Convert.ToDecimal(row.Value(nameof(location.LocHeight))) : 0;
            //        location.LocMaxOutsize = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocMaxOutsize))) ? Convert.ToDecimal(row.Value(nameof(location.LocMaxOutsize))) : 0;
            //        location.LocMinOutsize = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocMinOutsize))) ? Convert.ToDecimal(row.Value(nameof(location.LocMinOutsize))) : 0;
            //        location.LocName = row.Value(nameof(location.LocName));
            //        location.LocTireType = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocTireType))) ? Convert.ToInt32(row.Value(nameof(location.LocTireType))) : 0;
            //        location.LocType = !string.IsNullOrWhiteSpace(row.Value(nameof(location.LocType))) ? Convert.ToInt32(row.Value(nameof(location.LocType))) : 0;
            //        location.MaterialId = !string.IsNullOrWhiteSpace(row.Value(nameof(location.MaterialId))) ? Convert.ToInt64(row.Value(nameof(location.MaterialId))) : 0;
            //        location.MdTime = !string.IsNullOrWhiteSpace(row.Value(nameof(location.MdTime))) ? Convert.ToDateTime(row.Value(nameof(location.MdTime))) : null;
            //        location.OutEnable = !string.IsNullOrWhiteSpace(row.Value(nameof(location.OutEnable))) ? Convert.ToInt32(row.Value(nameof(location.OutEnable))) : 0;
            //        location.PlcCode = row.Value(nameof(location.PlcCode));
            //        location.TaskId = !string.IsNullOrWhiteSpace(row.Value(nameof(location.TaskId))) ? Convert.ToInt64(row.Value(nameof(location.TaskId))) : 0;
            //        location.TaskOccupy = !string.IsNullOrWhiteSpace(row.Value(nameof(location.TaskOccupy))) ? Convert.ToInt32(row.Value(nameof(location.TaskOccupy))) : 0;
            //        location.TaskOccupyTime = !string.IsNullOrWhiteSpace(row.Value(nameof(location.TaskOccupyTime))) ? Convert.ToDateTime(row.Value(nameof(location.TaskOccupyTime))) : null;
            //        location.UpdateTime = !string.IsNullOrWhiteSpace(row.Value(nameof(location.UpdateTime))) ? Convert.ToDateTime(row.Value(nameof(location.UpdateTime))) : null;
            //        location.UseFlag = !string.IsNullOrWhiteSpace(row.Value(nameof(location.UseFlag))) ? Convert.ToInt32(row.Value(nameof(location.UseFlag))) : 0;
            //        result.Add(location);
            //    }
            //}
            return result;
        }


        #region 出入库记录, 时间段必选

        public List<StockInOutRecordBeans> QueryStockInOutRecord(StockInOutCondition condition)
        {
            /// 参数验证
            /// 产品条码
            /// 字符串是否为：NUll Empty 空格
            /// 条件选择（？ ：）
            /// 整数值 允许为空，保证前端下拉列表不显示0值
            /// 减少数据库访问，一次查询所有，关联
            /// 相关表
            /// prod_product 
            /// prod_sku_product ?
            /// bak_wcs_task
            /// bak_wcs_task_cmd
            /// code_wcs_task_type (带Cmd)
            /// code_order_finish
            /// rcs_location
            /// prod_materail
            /// 

            if (condition.StartTime is null || condition.StartTime == DateTime.MinValue)
            {
                return new List<StockInOutRecordBeans>();
            }

            if (condition.EndTime is null || condition.EndTime == DateTime.MinValue)
            {
                return new List<StockInOutRecordBeans>();
            }


            var startTime = (condition.StartTime is null || condition.StartTime == DateTime.MinValue) ? DateTime.Now.AddMonths(-1) : condition.StartTime;
            var endTime = (condition.EndTime is null || condition.EndTime == DateTime.MinValue) ? DateTime.Now : condition.EndTime;

            var taskType = 1;
            if (condition.TaskType is not null && condition.TaskType > 0)//指定任务类型
            {
                taskType = condition.TaskType ?? 0;
            }


            var db = Db.Default;
            var CrnList = db.WcsEquipLocHolds.Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
            {
                EquipName = s.EquipName,
                LocId = f.LocId,
                EquipCode = s.Id
            }).AsEnumerable();

            #region 获取数据
            var result = db.BakWcsTaskCmds.Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime && t.CmdType == taskType)
                 .Join(db.CodeTaskTypes, f => f.TaskType, s => s.Id, (f, s) => new
                 {
                     TaskTypeName = s.CodeName,
                     CmdType = f.CmdType,
                     FinishTime = f.FinishTime,
                     CreateTime = f.CreateTime,
                     TaskCmd = f
                 })
                 .Where(f => f.TaskCmd.TaskType == (int)EnumTaskType.Warehousing || f.TaskCmd.TaskType == (int)EnumTaskType.Exwarehouse)//只显示出入库数据
                 .Join(db.BakWcsTasks, f => f.TaskCmd.TaskId, s => s.Id, (f, s) => new
                 {
                     TaskTypeName = f.TaskTypeName,
                     f.CmdType,
                     FinishTime = f.FinishTime,
                     CreateTime = f.CreateTime,
                     Task = s,
                     TaskCmd = f.TaskCmd
                 }).Where(t => t.Task.CreateTime >= startTime && t.Task.CreateTime <= endTime)
                .GroupJoin(db.RcsEquipCurings, f => f.Task.TargetEquip, s => s.Id, (f, s) => new
                {
                    TaskTypeName = f.TaskTypeName,
                    f.CmdType,
                    FinishTime = f.FinishTime,
                    CreateTime = f.CreateTime,
                    Task = f.Task,
                    TaskCmd = f.TaskCmd,
                    Curing = s,
                }).SelectMany(t => t.Curing.DefaultIfEmpty(), (f, s) => new
                {
                    TaskTypeName = f.TaskTypeName,
                    f.CmdType,
                    FinishTime = f.FinishTime,
                    CreateTime = f.CreateTime,
                    Task = f.Task,
                    TaskCmd = f.TaskCmd,
                    CuringModelIndex = s == null ? null : s.CuringModeIndex,
                    CuringSign = s == null ? null : s.CuringSign,
                    CuringCode = s == null ? null : s.CuringCode,
                })
                .Join(db.RcsLocations, f => f.Task.TargetLoc, s => s.Id, (f, s) => new
                {
                    TaskTypeName = f.TaskTypeName,
                    f.CmdType,
                    FinishTime = f.FinishTime,
                    CreateTime = f.CreateTime,
                    InLoc = s.LocCode,
                    EquipCode = s.LocCode,
                    CuringModelIndex = f.CuringModelIndex,
                    CuringSign = f.CuringSign,
                    Task = f.Task,
                    TaskCmd = f.TaskCmd,
                    CuringCode = f.CuringCode,
                })
               .Join(db.RcsLocations, f => f.Task.StartLoc, s => s.Id, (f, s) => new
               {
                   TaskTypeName = f.TaskTypeName,
                   f.CmdType,
                   FinishTime = f.FinishTime,
                   CreateTime = f.CreateTime,
                   InLoc = f.InLoc,
                   OutLoc = s.LocCode,
                   EquipCode = f.EquipCode,
                   CuringModelIndex = f.CuringModelIndex,
                   CuringSign = f.CuringSign,
                   Task = f.Task,
                   TaskCmd = f.TaskCmd,
                   CuringCode = f.CuringCode,
               })
               .GroupJoin(CrnList, f => f.TaskCmd.TargetLoc, s => s.LocId, (f, s) => new
               {
                   TaskTypeName = f.TaskTypeName,
                   f.CmdType,
                   FinishTime = f.FinishTime,
                   CreateTime = f.CreateTime,
                   InLoc = f.InLoc,
                   OutLoc = f.OutLoc,
                   EquipCode = f.EquipCode,
                   CuringModelIndex = f.CuringModelIndex,
                   CuringSign = f.CuringSign,
                   Task = f.Task,
                   TaskCmd = f.TaskCmd,
                   Crn = s,
                   CuringCode = f.CuringCode,
               }).SelectMany(t => t.Crn.DefaultIfEmpty(), (f, s) => new
               {
                   TaskTypeName = f.TaskTypeName,
                   f.CmdType,
                   FinishTime = f.FinishTime,
                   CreateTime = f.CreateTime,
                   InLoc = f.InLoc,
                   OutLoc = f.OutLoc,
                   EquipCode = f.EquipCode,
                   CuringModelIndex = f.CuringModelIndex,
                   CuringSign = f.CuringSign,
                   Task = f.Task,
                   TaskCmd = f.TaskCmd,
                   CrnName = s == null ? null : s.EquipName,
                   CrnCode = s == null ? null : s.EquipCode,
                   CuringCode = f.CuringCode,
               })
                .GroupJoin(CrnList, f => f.TaskCmd.StartLoc, s => s.LocId, (f, s) => new
                {
                    TaskTypeName = f.TaskTypeName,
                    f.CmdType,
                    FinishTime = f.FinishTime,
                    CreateTime = f.CreateTime,
                    InLoc = f.InLoc,
                    OutLoc = f.OutLoc,
                    EquipCode = f.EquipCode,
                    CuringModelIndex = f.CuringModelIndex,
                    CuringSign = f.CuringSign,
                    Task = f.Task,
                    TaskCmd = f.TaskCmd,
                    CrnName = f.CrnName,
                    Crn = s,
                    CuringCode = f.CuringCode,
                }).SelectMany(t => t.Crn.DefaultIfEmpty(), (f, s) => new
                {
                    TaskTypeName = f.TaskTypeName,
                    f.CmdType,
                    FinishTime = f.FinishTime,
                    CreateTime = f.CreateTime,
                    InLoc = f.InLoc,
                    OutLoc = f.OutLoc,
                    EquipCode = f.EquipCode,
                    CuringModelIndex = f.CuringModelIndex,
                    CuringSign = f.CuringSign,
                    Task = f.Task,
                    TaskCmd = f.TaskCmd,
                    CrnName = f.CrnName,
                    CrnCode = s == null ? null : s.EquipCode,
                    StartCrnName = s == null ? null : s.EquipName,
                    CuringCode = f.CuringCode,
                })
              .Join(db.CodeFinishModes, f => f.Task.FinishMode, s => s.Id, (f, s) => new
              {
                  TaskTypeName = f.TaskTypeName,
                  f.CmdType,
                  FinishTime = f.FinishTime,
                  CreateTime = f.CreateTime,
                  InLoc = f.InLoc,
                  OutLoc = f.OutLoc,
                  EquipCode = f.EquipCode,
                  CuringModelIndex = f.CuringModelIndex,
                  CuringSign = f.CuringSign,
                  FinishMode = s.CodeName,
                  Task = f.Task,
                  TaskCmd = f.TaskCmd,
                  CrnName = f.CrnName,
                  CrnCode = f.CrnCode,
                  StartCrnName = f.StartCrnName,
                  CuringCode = f.CuringCode,
              })
            .Join(db.RcsEquips, f => f.TaskCmd.EquipId, s => s.Id, (f, s) => new
            {
                TaskTypeName = f.TaskTypeName,
                f.CmdType,
                FinishTime = f.FinishTime,
                CreateTime = f.CreateTime,
                InLoc = f.InLoc,
                OutLoc = f.OutLoc,
                CuringModelIndex = f.CuringModelIndex,
                CuringSign = f.CuringSign,
                FinishMode = f.FinishMode,
                EquipName = s.EquipName,
                EquipCode = f.EquipCode,
                Task = f.Task,
                TaskCmd = f.TaskCmd,
                CrnName = f.CrnName,
                CrnCode = f.CrnCode,
                StartCrnName = f.StartCrnName,
                CuringCode = f.CuringCode,
            })
            .Join(db.ProdSkuProducts, f => f.Task.SkuId, s => s.SkuId, (f, s) => new
            {
                TaskTypeName = f.TaskTypeName,
                f.CmdType,
                FinishTime = f.FinishTime,
                CreateTime = f.CreateTime,
                InLoc = f.InLoc,
                OutLoc = f.OutLoc,
                CuringModelIndex = f.CuringModelIndex,
                CuringSign = f.CuringSign,
                FinishMode = f.FinishMode,
                EquipName = f.EquipName,
                EquipCode = f.EquipCode,
                Task = f.Task,
                TaskCmd = f.TaskCmd,
                CrnName = f.CrnName,
                CrnCode = f.CrnCode,
                StartCrnName = f.StartCrnName,
                ProductSku = s,
                CuringCode = f.CuringCode,
            })
            .Join(db.ProdProducts, f => f.ProductSku.ProductId, s => s.Id, (f, s) => new
            {
                TaskTypeName = f.TaskTypeName,
                f.CmdType,
                FinishTime = f.FinishTime,
                CreateTime = f.CreateTime,
                InLoc = f.InLoc,
                OutLoc = f.OutLoc,
                CuringModelIndex = f.CuringModelIndex,
                CuringSign = f.CuringSign,
                FinishMode = f.FinishMode,
                EquipName = f.EquipName,
                EquipCode = f.EquipCode,
                ProductCode = s.ProductCode,
                ProductQuality = s.GtStatus,
                Task = f.Task,
                TaskCmd = f.TaskCmd,
                Product = s,
                CrnName = f.CrnName,
                StartCrnName = f.StartCrnName,
                CrnCode = f.CrnCode,
                CuringCode = f.CuringCode,
            })
            .Join(db.ProdMaterials, f => f.Product.MaterialId, s => s.Id, (f, s) => new
            {
                TaskTypeName = f.TaskTypeName,
                f.CmdType,
                FinishTime = f.FinishTime,
                CreateTime = f.CreateTime,
                InLoc = f.InLoc,
                OutLoc = f.OutLoc,
                CuringModelIndex = f.CuringModelIndex,
                CuringSign = f.CuringSign,
                FinishMode = f.FinishMode,
                EquipName = f.EquipName,
                EquipCode = f.EquipCode,
                ProductCode = f.ProductCode,
                ProductQuality = f.ProductQuality,
                Spec = s.Spec,
                Task = f.Task,
                TaskCmd = f.TaskCmd,
                CrnName = f.CrnName,
                CrnCode = f.CrnCode,
                StartCrnName = f.StartCrnName,
                CuringCode = f.CuringCode ?? "",
                f.Product.MdTime,
            })
             .ToList();


            #endregion




            if (!string.IsNullOrWhiteSpace(condition.ProductCode))//指定产品条码
            {
                result = result.Where(f => f.ProductCode.Contains(condition.ProductCode.Trim())).ToList();
            }

            if (!string.IsNullOrWhiteSpace(condition.CuringCode))//指定硫化机
            {
                result = result.Where(f => f.CuringCode.Contains(condition.CuringCode)).ToList();
            }



            if (!string.IsNullOrWhiteSpace(condition.StockerCode))//根据堆垛机编号，指定堆垛机
            {
                result = result.Where(f => f.CrnCode > 0 && f.CrnCode == Convert.ToInt64(condition.StockerCode.Trim())).ToList();
            }

            if (!string.IsNullOrWhiteSpace(condition.CrnName.Trim()))//根据堆垛机名称，指定堆垛机
            {
                result = result.Where(f => f.CrnName == condition.CrnName.Trim() || f.StartCrnName == condition.CrnName.Trim()).ToList();
            }

            if (!string.IsNullOrWhiteSpace(condition.LocCode))//指定货位编码
            {
                result = result.Where(f => f.InLoc.Contains(condition.LocCode)).ToList();
            }

            if (!string.IsNullOrWhiteSpace(condition.Spec))
            {
                result = result.Where(f => f.Spec == condition.Spec.Trim()).ToList();
            }

            return result.Select(f => new StockInOutRecordBeans
            {
                ProductCode = f.ProductCode,//条码
                Spec = f.Spec,//物料名称

                Quality = f.ProductQuality == "0" ? "合格" : "不合格",//质量

                TaskType = f.TaskTypeName,//任务类型

                FinishTime = f.FinishTime,

                OutCreateTime = f.CmdType == 2 ? f.CreateTime : null,//出库创建时间
                OutFinishTime = f.CmdType == 2 ? f.FinishTime : null,//出库完成时间
                OutStockLocName = f.CmdType == 2 ? f.OutLoc : null,//出库取货位置名

                InCreateTime = f.CmdType == 1 ? f.CreateTime : null,//入库创建时间
                InFinishTime = f.CmdType == 1 ? f.FinishTime : null,//入库完成时间
                InStockLocName = f.CmdType == 1 ? f.InLoc : null,//入库放料位置名

                TargetEquip = f.CmdType == 2 ? f.EquipName : null,//目标设备名称

                //TargetEquipCode = f.CmdType == 2 ? f.EquipCode : null, //硫化机号
                TargetEquipCode = f.CmdType == 2 ? f.CuringCode : null, //硫化机号

                StockerName = f.CmdType == 1 ? f.CrnName : f.StartCrnName,//堆垛机名称

                LeftRightFlag = f.CuringModelIndex is null ? null : f.CuringSign == 1 ? (f.CuringModelIndex == 1 ? "R" : "L") : (f.CuringModelIndex == 1 ? "L" : "R"),//左右膜

                FinishMode = f.FinishMode,//结束类型
                BackTime = f.Task.BackTime,
                MdTime = f.MdTime,





            }).OrderByDescending(t => t.FinishTime).ToList();
        }

        public StockInOutRecordV2Beans QueryStockInOutRecordV3(StockInOutCondition condition)
        {
            /// 参数验证
            /// 产品条码
            /// 字符串是否为：NUll Empty 空格
            /// 条件选择（？ ：）
            /// 整数值 允许为空，保证前端下拉列表不显示0值
            /// 减少数据库访问，一次查询所有，关联
            /// 相关表
            /// prod_product 
            /// prod_sku_product ?
            /// bak_wcs_task
            /// bak_wcs_task_cmd
            /// code_wcs_task_type (带Cmd)
            /// code_order_finish
            /// rcs_location
            /// prod_materail
            /// 
            //WriteLog($"出入库400{condition.ToJson()}", true);
            if (condition.StartTime is null || condition.StartTime == DateTime.MinValue)
            {
                return new StockInOutRecordV2Beans();
            }

            if (condition.EndTime is null || condition.EndTime == DateTime.MinValue)
            {
                return new StockInOutRecordV2Beans();
            }

  
            var startTime = (condition.StartTime is null || condition.StartTime == DateTime.MinValue) ? DateTime.Now.AddMonths(-1) : condition.StartTime;
            var endTime = (condition.EndTime is null || condition.EndTime == DateTime.MinValue) ? DateTime.Now : condition.EndTime;

            var taskType = 0;
            if (condition.TaskType is not null && condition.TaskType >= 0)//指定任务类型
            {
                taskType = condition.TaskType ?? 0;
            }
            var page = condition.Page1 ?? 1;
            var limit = condition.Limit1 ?? 10;


            var db = Db.Default;
            //var CrnList = db.WcsEquipLocHolds.Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
            //{
            //    s.EquipName,
            //    f.LocId,
            //    EquipCode = s.Id
            //}).AsEnumerable();



            #region 获取数据
            var result = db.BakWcsTaskCmds.Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime )
                 .Join(db.CodeTaskTypes, f => f.TaskType, s => s.Id, (f, s) => new
                 {
                     TaskTypeName = s.CodeName,
                     f.CmdType,
                     f.FinishTime,
                     f.CreateTime,
                     f.TaskId,
                     f.EquipId,
                 }).Join(db.BakWcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
                 {
                     f.TaskTypeName,
                     f.CmdType,
                     f.FinishTime,
                     f.CreateTime,
                     f.TaskId,
                     TaskCreateTime = s.CreateTime,
                     s.TargetEquip,
                     s.BackTime,
                     s.TargetLoc,
                     s.StartLoc,
                     f.EquipId,
                     s.SkuId,
                     s.FinishMode,
                 }).Where(t => t.TaskCreateTime >= startTime && t.TaskCreateTime <= endTime)
                .GroupJoin(db.RcsEquipCurings, f => f.TargetEquip, s => s.Id, (f, s) => new
                {
                    f.TaskTypeName,
                    f.CmdType,
                    f.FinishTime,
                    f.CreateTime,
                    f.TaskId,
                    f.TaskCreateTime,
                    f.TargetEquip,
                    f.BackTime,
                    f.TargetLoc,
                    f.StartLoc,
                    f.EquipId,
                    f.SkuId,
                    f.FinishMode,
                    Curing = s,
                }).SelectMany(t => t.Curing.DefaultIfEmpty(), (f, s) => new
                {
                    f.TaskTypeName,
                    f.CmdType,
                    f.FinishTime,
                    f.CreateTime,
                    f.TaskId,
                    f.TaskCreateTime,
                    f.TargetEquip,
                    f.BackTime,
                    f.TargetLoc,
                    f.StartLoc,
                    f.EquipId,
                    f.SkuId,
                    f.FinishMode,
                    CuringModelIndex = s == null ? null : s.CuringModeIndex,
                    CuringSign = s == null ? null : s.CuringSign,
                    CuringCode = s == null ? null : s.CuringCode,
                })
                .Join(db.RcsLocations, f => f.TargetLoc, s => s.Id, (f, s) => new
                {
                    f.TaskTypeName,
                    f.CmdType,
                    f.FinishTime,
                    f.CreateTime,
                    f.TaskId,
                    f.TaskCreateTime,
                    f.TargetEquip,
                    f.BackTime,
                    f.TargetLoc,
                    f.StartLoc,
                    f.EquipId,
                    f.SkuId,
                    f.CuringModelIndex,
                    f.CuringSign,
                    f.CuringCode,
                    f.FinishMode,
                    InLoc = s.LocName,


                })
               .Join(db.RcsLocations, f => f.StartLoc, s => s.Id, (f, s) => new
               {
                   f.TaskTypeName,
                   f.CmdType,
                   f.FinishTime,
                   f.CreateTime,
                   f.TaskId,
                   f.TaskCreateTime,
                   f.TargetEquip,
                   f.BackTime,
                   f.TargetLoc,
                   f.StartLoc,
                   f.EquipId,
                   f.SkuId,
                   f.CuringModelIndex,
                   f.CuringSign,
                   f.CuringCode,
                   f.InLoc,
                   f.FinishMode,
                   OutLoc = s.LocName,
               })
              .Join(db.CodeFinishModes, f => f.FinishMode, s => s.Id, (f, s) => new
              {
                  f.TaskTypeName,
                  f.CmdType,
                  f.FinishTime,
                  f.CreateTime,
                  f.TaskId,
                  f.TaskCreateTime,
                  f.TargetEquip,
                  f.BackTime,
                  f.TargetLoc,
                  f.StartLoc,
                  f.EquipId,
                  f.SkuId,
                  f.CuringModelIndex,
                  f.CuringSign,
                  f.CuringCode,
                  f.InLoc,
                  f.OutLoc,
                  FinishMode = s.CodeName,

              })
            .Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
            {
                f.TaskTypeName,
                f.CmdType,
                f.FinishTime,
                f.CreateTime,
                f.TaskId,
                f.TaskCreateTime,
                f.TargetEquip,
                f.BackTime,
                f.TargetLoc,
                f.StartLoc,
                f.EquipId,
                f.SkuId,
                f.CuringModelIndex,
                f.CuringSign,
                f.CuringCode,
                f.InLoc,
                f.OutLoc,
                f.FinishMode,
                s.EquipName,
            })
            .Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
            {
                f.TaskTypeName,
                f.CmdType,
                f.FinishTime,
                f.CreateTime,
                f.TaskId,
                f.TaskCreateTime,
                f.TargetEquip,
                f.BackTime,
                f.TargetLoc,
                f.StartLoc,
                f.EquipId,
                f.SkuId,
                f.CuringModelIndex,
                f.CuringSign,
                f.CuringCode,
                f.InLoc,
                f.OutLoc,
                f.FinishMode,
                f.EquipName,
                s.ProductId,
            })
            .Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
            {
                f.TaskTypeName,
                f.CmdType,
                f.FinishTime,
                f.CreateTime,
                f.TaskId,
                f.TaskCreateTime,
                f.TargetEquip,
                f.BackTime,
                f.TargetLoc,
                f.StartLoc,
                f.EquipId,
                f.SkuId,
                f.CuringModelIndex,
                f.CuringSign,
                f.CuringCode,
                f.InLoc,
                f.OutLoc,
                f.FinishMode,
                f.EquipName,
                f.ProductId,
                s.MaterialId,
                s.ProductCode,
                s.ProductQuality,
                s.MdTime,
                s.MesId,
            })
            .Join(db.MesProducts, f => f.MesId, s => s.Id, (f, s) => new
            {
                f.TaskTypeName,
                f.CmdType,
                f.FinishTime,
                f.CreateTime,
                f.TaskId,
                f.TaskCreateTime,
                f.TargetEquip,
                f.BackTime,
                f.TargetLoc,
                f.StartLoc,
                f.EquipId,
                f.SkuId,
                f.CuringModelIndex,
                f.CuringSign,
                f.CuringCode,
                f.InLoc,
                f.OutLoc,
                f.FinishMode,
                f.EquipName,
                f.ProductId,
                f.MaterialId,
                f.ProductCode,
                f.ProductQuality,
                f.MdTime,
                s.TbmNo,
            })
            .Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
            {
                f.TaskTypeName,
                f.CmdType,
                f.FinishTime,
                f.CreateTime,
                f.TaskId,
                f.TaskCreateTime,
                f.TargetEquip,
                f.BackTime,
                f.TargetLoc,
                f.StartLoc,
                f.EquipId,
                f.SkuId,
                f.CuringModelIndex,
                f.CuringSign,
                f.CuringCode,
                f.InLoc,
                f.OutLoc,
                f.FinishMode,
                f.EquipName,
                f.ProductId,
                f.MaterialId,
                f.ProductCode,
                f.ProductQuality,
                f.MdTime,
                f.TbmNo,
                s.Spec,
            });


            #endregion
            if (taskType != 0)
            {
                result = result.Where(f => f.CmdType == taskType);
            }



            if (!string.IsNullOrWhiteSpace(condition.ProductCode))//指定产品条码
            {
                result = result.Where(f => f.ProductCode.Contains(condition.ProductCode.Trim()));
            }

            if (!string.IsNullOrWhiteSpace(condition.CuringCode))//指定硫化机
            {
                result = result.Where(f => f.CuringCode.Contains(condition.CuringCode));
            }



            //if (!string.IsNullOrWhiteSpace(condition.StockerCode))//根据堆垛机编号，指定堆垛机
            //{
            //    result = result.Where(f => f.CrnCode > 0 && f.CrnCode == Convert.ToInt64(condition.StockerCode.Trim())).ToList();
            //}

            if (!string.IsNullOrWhiteSpace(condition.CrnName.Trim()))//根据堆垛机名称，指定堆垛机
            {
                var crnName = condition.CrnName.Trim();
                result = result.Where(t => t.EquipName.StartsWith(crnName));
            }

            if (!string.IsNullOrWhiteSpace(condition.LocCode))//指定货位编码
            {
                result = result.Where(f => f.InLoc.Contains(condition.LocCode));
            }

            if (!string.IsNullOrWhiteSpace(condition.Spec))
            {
                result = result.Where(f => f.Spec == condition.Spec.Trim());
            }
            var start = (page - 1) * limit;
            var data = result.OrderBy(t => t.CreateTime).Skip(start).Take(limit).ToList();

            StockInOutRecordV2Beans bean = new StockInOutRecordV2Beans();
            bean.total = result.Count();
            bean.len = limit;
            bean.start = start;



            var rows = data.Select(f => new Row
            {
                ProductCode = f.ProductCode,//条码
                Spec = f.Spec,//物料名称

                Quality = f.ProductQuality == 0 ? "合格" : "不合格",//质量

                TaskType = f.TaskTypeName,//任务类型

                FinishTime = f.FinishTime,

                //OutCreateTime = f.CmdType == 2 ? f.CreateTime : null,//出库创建时间
                OutFinishTime = f.CmdType == 2 ? f.FinishTime : null,//出库完成时间
                OutStockLocName = f.CmdType == 2 ? f.OutLoc : null,//出库取货位置名

                //InCreateTime = f.CmdType == 1 ? f.CreateTime : null,//入库创建时间
                InFinishTime = f.CmdType == 1 ? f.FinishTime : null,//入库完成时间
                InStockLocName = f.CmdType == 1 ? f.InLoc : null,//入库放料位置名

                TbmNo = f.TbmNo,

                TargetEquip = f.CmdType == 2 ? f.EquipName : null,//目标设备名称

                //TargetEquipCode = f.CmdType == 2 ? f.EquipCode : null, //硫化机号
                TargetEquipCode = f.CmdType == 2 ? f.CuringCode : null, //硫化机号

                StockerName = f.CmdType == 1 ? f.InLoc.Split('机')[0].ToString() + "机" : f.OutLoc.Split('机')[0].ToString() + "机",//堆垛机名称

                LeftRightFlag = f.CuringModelIndex is null ? null : f.CuringSign == 1 ? (f.CuringModelIndex == 1 ? "R" : "L") : (f.CuringModelIndex == 1 ? "L" : "R"),//左右膜
                CreateTime = f.CreateTime,

                FinishMode = f.FinishMode,//结束类型
                BackTime = f.BackTime,
                MdTime = f.MdTime,
            }).ToList();



            bean.rows = rows;


            //WriteLog($"出入库738{rows.ToJson()}", true);
            return bean;
        }


        //public List<StockInOutRecordV2Beans> QueryStockInOutRecordV2(StockInOutCondition condition)
        //{

        //    WriteLog("出入库查询开始执行1", true);
        //    if (condition.StartTime is null || condition.StartTime == DateTime.MinValue)
        //    {
        //        return new List<StockInOutRecordV2Beans>();
        //    }

        //    if (condition.EndTime is null || condition.EndTime == DateTime.MinValue)
        //    {
        //        return new List<StockInOutRecordV2Beans>();
        //    }

        //    WriteLog("出入库查询开始执行396", true);
        //    var startTime = (condition.StartTime is null || condition.StartTime == DateTime.MinValue) ? DateTime.Now.AddMonths(-1) : condition.StartTime;
        //    var endTime = (condition.EndTime is null || condition.EndTime == DateTime.MinValue) ? DateTime.Now : condition.EndTime;
        //    WriteLog("出入库查询开始执行399", true);
        //    var taskType = 1;
        //    if (condition.TaskType is not null && condition.TaskType > 0)//指定任务类型
        //    {
        //        taskType = condition.TaskType ?? 0;
        //    }

        //    WriteLog("出入库查询开始执行406", true);
        //    var db = Db.Default;



        //    var result = db.BakWcsTaskCmds.Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime && t.CmdType == taskType)
        //         .Join(db.CodeTaskTypes, f => f.TaskType, s => s.Id, (f, s) => new
        //         {
        //             TaskTypeName = s.CodeName,
        //             f.CmdType,
        //             f.FinishTime,
        //             f.CreateTime,
        //             f.TaskType,
        //             f.TaskId,
        //             f.EquipId,
        //         })
        //        .Where(f => f.TaskType == (int)EnumTaskType.Warehousing || f.TaskType == (int)EnumTaskType.Exwarehouse)//只显示出入库数据
        //         .Join(db.BakWcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
        //         {
        //             f.TaskTypeName,
        //             f.CmdType,
        //             f.FinishTime,
        //             f.CreateTime,
        //             f.TaskType,
        //             f.TaskId,
        //             f.EquipId,
        //             TaskCreateTime = s.CreateTime,
        //             s.TargetEquip,

        //         }).Where(t => t.TaskCreateTime >= startTime && t.TaskCreateTime <= endTime)
        //        .GroupJoin(db.RcsEquipCurings, f => f.TargetEquip, s => s.Id, (f, s) => new
        //        {
        //            f.TaskTypeName,
        //            f.CmdType,
        //            f.FinishTime,
        //            f.CreateTime,
        //            f.TaskType,
        //            f.TaskId,
        //            f.EquipId,
        //            f.TaskCreateTime,
        //            f.TargetEquip,
        //            Curing = s,
        //        }).SelectMany(t => t.Curing.DefaultIfEmpty(), (f, s) => new
        //        {
        //            f.TaskTypeName,
        //            f.CmdType,
        //            f.FinishTime,
        //            f.CreateTime,
        //            f.TaskType,
        //            f.TaskId,
        //            f.EquipId,
        //            f.TaskCreateTime,
        //            f.TargetEquip,
        //            CuringModelIndex = s == null ? null : s.CuringModeIndex,
        //            CuringSign = s == null ? null : s.CuringSign,
        //            CuringCode = s == null ? null : s.CuringCode,
        //        })
        //        //.Join(db.BakWcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
        //        //{
        //        //    f.TaskTypeName,
        //        //    f.CmdType,
        //        //    f.FinishTime,
        //        //    f.CreateTime,
        //        //    TaskCreateTime = s.CreateTime,
        //        //    s.TargetEquip,
        //        //    s.BackTime,
        //        //    s.TargetLoc,
        //        //    s.StartLoc,
        //        //    s.FinishMode,
        //        //    f.EquipId,
        //        //    s.SkuId,
        //        //    CuringModelIndex = 0,
        //        //    CuringSign = 0,
        //        //    CuringCode = 0,
        //        //    //TaskCmd = f.TaskCmd
        //        //}).Where(t => t.TaskCreateTime >= startTime && t.TaskCreateTime <= endTime)

        //        .Join(db.RcsLocations, f => f.TargetLoc, s => s.Id, (f, s) => new
        //        {
        //            f.TaskTypeName,
        //            f.CmdType,
        //            f.FinishTime,
        //            f.CreateTime,
        //            InLoc = s.LocName,
        //            EquipCode = s.LocCode,
        //            f.CuringModelIndex,
        //            f.CuringSign,
        //            f.StartLoc,
        //            f.TargetLoc,
        //            f.FinishMode,
        //            f.EquipId,
        //            f.SkuId,
        //            f.CuringCode,
        //            f.BackTime,
        //            s.LocType,
        //        }).Where(t => t.LocType == (int)EnumLocType.Location)
        //      .Join(db.CodeFinishModes, f => f.FinishMode, s => s.Id, (f, s) => new
        //      {
        //          f.TaskTypeName,
        //          f.CmdType,
        //          f.FinishTime,
        //          f.CreateTime,
        //          f.InLoc,
        //          OutLoc = "",
        //          f.EquipCode,
        //          f.CuringModelIndex,
        //          f.CuringSign,
        //          FinishMode = s.CodeName,
        //          CrnName = "",//.f.CrnName,
        //          CrnCode = 0,
        //          StartCrnName = "",
        //          f.CuringCode,
        //          f.EquipId,
        //          f.SkuId,
        //          f.BackTime,
        //      })
        //    .Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
        //    {
        //        f.TaskTypeName,
        //        f.CmdType,
        //        f.FinishTime,
        //        f.CreateTime,
        //        f.InLoc,
        //        f.OutLoc,
        //        f.CuringModelIndex,
        //        f.CuringSign,
        //        f.FinishMode,
        //        s.EquipName,
        //        f.EquipCode,
        //        f.CrnName,
        //        f.CrnCode,
        //        f.StartCrnName,
        //        f.CuringCode,
        //        f.EquipId,
        //        f.SkuId,
        //        f.BackTime,
        //    })
        //    .Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
        //    {
        //        f.TaskTypeName,
        //        f.CmdType,
        //        f.FinishTime,
        //        f.CreateTime,
        //        f.InLoc,
        //        f.OutLoc,
        //        f.CuringModelIndex,
        //        f.CuringSign,
        //        f.FinishMode,
        //        f.EquipName,
        //        f.EquipCode,
        //        f.CrnName,
        //        f.CrnCode,
        //        f.StartCrnName,
        //        s.ProductId,
        //        f.CuringCode,
        //        f.BackTime,
        //    })
        //    .Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
        //    {
        //        f.TaskTypeName,
        //        f.CmdType,
        //        f.FinishTime,
        //        f.CreateTime,
        //        f.InLoc,
        //        f.OutLoc,
        //        f.CuringModelIndex,
        //        f.CuringSign,
        //        f.FinishMode,
        //        f.EquipName,
        //        f.EquipCode,
        //        s.ProductCode,
        //        ProductQuality = s.GtStatus,
        //        s.MaterialId,
        //        f.CrnName,
        //        f.StartCrnName,
        //        f.CrnCode,
        //        f.CuringCode,
        //        s.MdTime,
        //        f.BackTime,
        //    })
        //    .Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
        //    {
        //        f.TaskTypeName,
        //        f.CmdType,
        //        f.FinishTime,
        //        f.CreateTime,
        //        f.InLoc,
        //        f.OutLoc,
        //        f.CuringModelIndex,
        //        f.CuringSign,
        //        f.FinishMode,
        //        f.EquipName,
        //        f.EquipCode,
        //        f.ProductCode,
        //        f.ProductQuality,
        //        s.Spec,
        //        f.CrnName,
        //        f.CrnCode,
        //        f.StartCrnName,
        //        CuringCode = "",
        //        f.MdTime,
        //        f.BackTime,

        //    }).Select(f => new StockInOutRecordV2Beans
        //    {
        //        ProductCode = f.ProductCode,//条码
        //        Spec = f.Spec,//物料名称
        //        Quality = f.ProductQuality == "0" ? "合格" : "不合格",//质量
        //        TaskType = f.TaskTypeName,//任务类型
        //        FinishTime = f.FinishTime,
        //        OutFinishTime = f.CmdType == 2 ? f.FinishTime : null,//出库完成时间
        //        OutStockLocName = f.CmdType == 2 ? f.OutLoc : null,//出库取货位置名
        //        InFinishTime = f.CmdType == 1 ? f.FinishTime : null,//入库完成时间
        //        InStockLocName = f.CmdType == 1 ? f.InLoc : null,//入库放料位置名
        //        TargetEquip = f.CmdType == 2 ? f.EquipName : null,//目标设备名称
        //        TargetEquipCode = f.CmdType == 2 ? f.CuringCode : null, //硫化机号
        //        StockerName = f.CmdType == 1 ? f.CrnName : f.StartCrnName,//堆垛机名称
        //        FinishMode = f.FinishMode,//结束类型
        //        BackTime = f.BackTime,
        //        MdTime = f.MdTime,
        //    }).OrderByDescending(f => f.FinishTime).Take(10).ToList();

        //    WriteLog("出入库查询开始执行613", true);
        //    if (!string.IsNullOrWhiteSpace(condition.ProductCode))//指定产品条码
        //    {
        //        result = result.Where(f => f.ProductCode.Contains(condition.ProductCode.Trim())).ToList();
        //    }

        //    if (!string.IsNullOrWhiteSpace(condition.CuringCode))//指定硫化机
        //    {
        //        result = result.Where(f => f.TargetEquipCode.Contains(condition.CuringCode)).ToList();
        //    }



        //    if (!string.IsNullOrWhiteSpace(condition.StockerCode))//根据堆垛机编号，指定堆垛机
        //    {
        //        result = result.Where(f => f.CrnCode > 0 && f.CrnCode == Convert.ToInt64(condition.StockerCode.Trim())).ToList();
        //    }

        //    if (!string.IsNullOrWhiteSpace(condition.CrnName.Trim()))//根据堆垛机名称，指定堆垛机
        //    {
        //        result = result.Where(f => f.CrnName == condition.CrnName.Trim() || f.StartCrnName == condition.CrnName.Trim()).ToList();
        //    }

        //    if (!string.IsNullOrWhiteSpace(condition.LocCode))//指定货位编码
        //    {
        //        result = result.Where(f => f.InLoc.Contains(condition.LocCode)).ToList();
        //    }

        //    if (!string.IsNullOrWhiteSpace(condition.Spec))
        //    {
        //        result = result.Where(f => f.Spec == condition.Spec.Trim()).ToList();
        //    }

        //    WriteLog("出入库查询开始执行683", true);
        //    return result;








        //    //#region 获取数据
        //    //var result = db.BakWcsTaskCmds.Where(t => t.CreateTime >= startTime && t.CreateTime <= endTime && t.CmdType == taskType)
        //    //     .Join(db.CodeTaskTypes, f => f.TaskType, s => s.Id, (f, s) => new
        //    //     {
        //    //         TaskTypeName = s.CodeName,
        //    //         CmdType = f.CmdType,
        //    //         f.FinishTime,
        //    //         f.CreateTime,
        //    //         f.TaskType,
        //    //         f.TaskId,
        //    //         f.EquipId,
        //    //     })
        //    //     .Where(f => f.CmdType == (int)EnumTaskType.Warehousing || f.TaskType == (int)EnumTaskType.Exwarehouse)//只显示出入库数据
        //    //     .Join(db.BakWcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
        //    //     {
        //    //         f.TaskTypeName,
        //    //         f.CmdType,
        //    //         f.FinishTime,
        //    //         f.CreateTime,
        //    //         TaskCreateTime = s.CreateTime,
        //    //         s.TargetEquip,
        //    //         s.BackTime,
        //    //         s.TargetLoc,
        //    //         s.StartLoc,
        //    //         s.FinishMode,
        //    //         f.EquipId,
        //    //         s.SkuId,
        //    //         //TaskCmd = f.TaskCmd
        //    //     }).Where(t => t.TaskCreateTime >= startTime && t.TaskCreateTime <= endTime)
        //    //    .GroupJoin(db.RcsEquipCurings, f => f.TargetEquip, s => s.Id, (f, s) => new
        //    //    {
        //    //        f.TaskTypeName,
        //    //        f.CmdType,
        //    //        f.FinishTime,
        //    //        f.CreateTime,
        //    //        f.BackTime,
        //    //        f.TargetLoc,
        //    //        f.StartLoc,
        //    //        f.FinishMode,
        //    //        f.EquipId,
        //    //        f.SkuId,
        //    //        Curing = s,
        //    //    }).SelectMany(t => t.Curing.DefaultIfEmpty(), (f, s) => new
        //    //    {
        //    //        f.TaskTypeName,
        //    //        f.CmdType,
        //    //        f.FinishTime,
        //    //        f.CreateTime,
        //    //        f.TargetLoc,
        //    //        f.StartLoc,
        //    //        f.FinishMode,
        //    //        f.EquipId,
        //    //        f.SkuId,
        //    //        f.BackTime,
        //    //        CuringModelIndex = s == null ? null : s.CuringModeIndex,
        //    //        CuringSign = s == null ? null : s.CuringSign,
        //    //        CuringCode = s == null ? null : s.CuringCode,
        //    //    })
        //    //    .Join(db.RcsLocations, f => f.TargetLoc, s => s.Id, (f, s) => new
        //    //    {
        //    //        f.TaskTypeName,
        //    //        f.CmdType,
        //    //        f.FinishTime,
        //    //        f.CreateTime,
        //    //        InLoc = s.LocName,
        //    //        EquipCode = s.LocCode,
        //    //        f.CuringModelIndex,
        //    //        f.CuringSign,
        //    //        f.StartLoc,
        //    //        f.TargetLoc,
        //    //        f.FinishMode,
        //    //        f.EquipId,
        //    //        f.SkuId,
        //    //        f.CuringCode,
        //    //        f.BackTime,
        //    //    }).Join(db.RcsLocations, f => f.StartLoc, s => s.Id, (f, s) => new
        //    //    {
        //    //        f.TaskTypeName,
        //    //        f.CmdType,
        //    //        f.FinishTime,
        //    //        f.CreateTime,
        //    //        f.InLoc,
        //    //        OutLoc = s.LocCode,
        //    //        f.EquipCode,
        //    //        f.CuringModelIndex,
        //    //        f.CuringSign,
        //    //        f.CuringCode,
        //    //        f.StartLoc,
        //    //        f.TargetLoc,
        //    //        f.EquipId,
        //    //        f.SkuId,
        //    //        f.FinishMode,
        //    //        f.BackTime,
        //    //    })
        //    //  .Join(db.CodeFinishModes, f => f.FinishMode, s => s.Id, (f, s) => new
        //    //  {
        //    //      f.TaskTypeName,
        //    //      f.CmdType,
        //    //      f.FinishTime,
        //    //      f.CreateTime,
        //    //      f.InLoc,
        //    //      f.OutLoc,
        //    //      f.EquipCode,
        //    //      f.CuringModelIndex,
        //    //      f.CuringSign,
        //    //      FinishMode = s.CodeName,
        //    //      CrnName = "",//.f.CrnName,
        //    //      CrnCode = 0,
        //    //      StartCrnName = "",
        //    //      f.CuringCode,
        //    //      f.EquipId,
        //    //      f.SkuId,
        //    //      f.BackTime,
        //    //  })
        //    //.Join(db.RcsEquips, f => f.EquipId, s => s.Id, (f, s) => new
        //    //{
        //    //    f.TaskTypeName,
        //    //    f.CmdType,
        //    //    f.FinishTime,
        //    //    f.CreateTime,
        //    //    f.InLoc,
        //    //    f.OutLoc,
        //    //    f.CuringModelIndex,
        //    //    f.CuringSign,
        //    //    f.FinishMode,
        //    //    s.EquipName,
        //    //    f.EquipCode,
        //    //    f.CrnName,
        //    //    f.CrnCode,
        //    //    f.StartCrnName,
        //    //    f.CuringCode,
        //    //    f.EquipId,
        //    //    f.SkuId,
        //    //    f.BackTime,
        //    //})
        //    //.Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
        //    //{
        //    //    f.TaskTypeName,
        //    //    f.CmdType,
        //    //    f.FinishTime,
        //    //    f.CreateTime,
        //    //    f.InLoc,
        //    //    f.OutLoc,
        //    //    f.CuringModelIndex,
        //    //    f.CuringSign,
        //    //    f.FinishMode,
        //    //    f.EquipName,
        //    //    f.EquipCode,
        //    //    f.CrnName,
        //    //    f.CrnCode,
        //    //    f.StartCrnName,
        //    //    s.ProductId,
        //    //    f.CuringCode,
        //    //    f.BackTime,
        //    //})
        //    //.Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
        //    //{
        //    //    f.TaskTypeName,
        //    //    f.CmdType,
        //    //    f.FinishTime,
        //    //    f.CreateTime,
        //    //    f.InLoc,
        //    //    f.OutLoc,
        //    //    f.CuringModelIndex,
        //    //    f.CuringSign,
        //    //    f.FinishMode,
        //    //    f.EquipName,
        //    //    f.EquipCode,
        //    //    s.ProductCode,
        //    //    ProductQuality = s.GtStatus,
        //    //    s.MaterialId,
        //    //    f.CrnName,
        //    //    f.StartCrnName,
        //    //    f.CrnCode,
        //    //    f.CuringCode,
        //    //    s.MdTime,
        //    //    f.BackTime,
        //    //})
        //    //.Join(db.ProdMaterials, f => f.MaterialId, s => s.Id, (f, s) => new
        //    //{
        //    //    f.TaskTypeName,
        //    //    f.CmdType,
        //    //    f.FinishTime,
        //    //    f.CreateTime,
        //    //    f.InLoc,
        //    //    f.OutLoc,
        //    //    f.CuringModelIndex,
        //    //    f.CuringSign,
        //    //    f.FinishMode,
        //    //    f.EquipName,
        //    //    f.EquipCode,
        //    //    f.ProductCode,
        //    //    f.ProductQuality,
        //    //    s.Spec,
        //    //    f.CrnName,
        //    //    f.CrnCode,
        //    //    f.StartCrnName,
        //    //    CuringCode = f.CuringCode ?? "",
        //    //    f.MdTime,
        //    //    f.BackTime,

        //    //}).ToList();

        //    //WriteLog("出入库查询开始执行613", true);
        //    //#endregion

        //}

        public List<StockInOutRecordDateBeans> QueryStockInOutRecordByDate(StockInOutDateCondition condition)
        {
            var result = new List<StockInOutRecordDateBeans>();
            var date = condition.Date;
            var db = Db.Default;
            if (String.IsNullOrWhiteSpace(date))
            {
                return null;
            }
            DateTime.TryParse($"{date} 08:30:00", out var dstartTime);
            var dendTime = dstartTime.AddDays(1);

            #region 获取数据
            var datas = db.BakWcsTasks.Where(t => t.CreateTime >= dstartTime && t.CreateTime <= dendTime && t.TaskStatus == 9 && (t.TaskType == (int)EnumTaskType.Warehousing || t.TaskType == (int)EnumTaskType.EMS))
               .Select(t => new
               {
                   t.TaskType,
                   t.CreateTime,
                   t.DirectDelivery,
               });
            #endregion


            DateTime.TryParse($"{date} 08:30:00", out var startTime);

            //var hours = (DateTime.Now - startTime).Hours;
            //int hour = hours > 24 ? 24 : hours;

            for (var i = 1; i <= 24; i++)
            {
                StockInOutRecordDateBeans bean = new();
                var endTime = startTime.AddHours(1);
                //查入库数量
                var inNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.TaskType == (int)EnumTaskType.Warehousing && t.DirectDelivery == 0).Count();
                var outNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.TaskType == (int)EnumTaskType.EMS && t.DirectDelivery == 0).Count();
                var directNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.DirectDelivery == 1).Count();
                if (inNum + outNum + directNum > 0)
                {
                    bean.Hour = startTime.ToString("yyyy-MM-dd HH:mm");
                    bean.InNum = inNum;
                    bean.OutNum = outNum;
                    bean.DirectCount = directNum;
                    result.Add(bean);
                }
                startTime = startTime.AddHours(1);
            }
            return result;
        }
        public List<StockInOutRecordDateBeans> QueryMinStockInOutRecordByDate(StockInOutDateCondition condition)
        {
            var result = new List<StockInOutRecordDateBeans>();
            var date = condition.Date;
            var db = Db.Default;
            if (String.IsNullOrWhiteSpace(date))
            {
                return null;
            }
            DateTime.TryParse($"{date} 08:30:00", out var dstartTime);
            var dendTime = dstartTime.AddDays(1);

            #region 获取数据
            var datas = db.BakWcsTasks.Where(t => t.CreateTime >= dstartTime && t.CreateTime <= dendTime && t.TaskStatus == 9 && (t.TaskType == (int)EnumTaskType.Warehousing || t.TaskType == (int)EnumTaskType.EMS))
               .Select(t => new
               {
                   t.TaskType,
                   t.CreateTime,
                   t.DirectDelivery,
               });
            #endregion


            DateTime.TryParse($"{date} 08:30:00", out var startTime);

            //var hours = (DateTime.Now - startTime).Hours;
            //int hour = hours > 24 ? 24 : hours;

            for (var i = 1; i <= 144; i++)
            {
                StockInOutRecordDateBeans bean = new();
                var endTime = startTime.AddMinutes(10);
                //查入库数量
                var inNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.TaskType == (int)EnumTaskType.Warehousing && t.DirectDelivery == 0).Count();
                var outNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.TaskType == (int)EnumTaskType.EMS && t.DirectDelivery == 0).Count();
                var directNum = datas.Where(t => t.CreateTime >= startTime && t.CreateTime < endTime && t.DirectDelivery == 1).Count();
                if (inNum + outNum + directNum > 0)
                {
                    bean.Hour = startTime.ToString("yyyy-MM-dd HH:mm");
                    bean.InNum = inNum;
                    bean.OutNum = outNum;
                    bean.DirectCount = directNum;
                    result.Add(bean);
                }
                startTime = startTime.AddMinutes(10);
            }
            return result;
        }

        #endregion



    }
}
